import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var Viewer: any;
@Component({
	selector: 'sa-feedback',
	templateUrl: './feedback.component.html',
	styleUrls: ['./feedback.css']
})
export class FeedBackComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		feedbackPhone:'',
		searchKey: '',
		startTime: '',
		endTime: '',
		state: ''
	}
	isViewer:boolean = false; //默认隐藏
	isShowDelete=false; // 默认不显示删除按钮
	title:any = "";  // 展示回复的标题
	content:any = "";  // 展示回复的内容
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
		System.import('assets/js/viewer/viewer.js').then(
			()=>{System.import('assets/js/viewer/bootstrap.bundle.min.js').then(
				()=>{System.import('assets/js/viewer/viewer.css').then(()=>{
				})
			})
		})
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:adviseLog')));
		if(!btnData) {
			return
		};
		for(let item of btnData) {
			if(item.sourceKey === "wx-bind:advise-log:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}
		}
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
                // feedbackPhone: this.tableParam.feedbackPhone,
				searchKey:common.stringTrim(this.tableParam.searchKey),
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime,
				status: this.tableParam.state,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("feedback/findFeedBackList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				let list = data.exhibitDatas;
				for (let index = 0; index < list.length; index++) {
					const item = list[index];
					// console.log(item.suggest.length)
					if(item.problemDes.length>30){
						item.suggestStr = item.problemDes.substring(0,30)+"..."
						item.suggestBtn = true;
					}else{
						item.suggestStr =item.problemDes
						item.suggestBtn = false;
					}
					// if(item.replyContent.length>30){
					// 	item.replyContentStr = item.replyContent.substring(0,30)+"..."
					// 	item.replyBtn = true;
					// }else{
					// 	item.replyContentStr =item.replyContent
					// 	item.replyBtn = false;
					// }
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	imgInfo: any=[];
	//查看照片
	open_img(row,val){
		var that = this;

		val.show();
		var imgList = row.imgPath.split(",");
		this.imgInfo = [];
		for(var b=0;b<imgList.length;b++){
			
			// let localImg;
			// if(imgList[b][0] == '/'){
			// 	localImg = config.BaseURL + imgList[b].substr(1);
			// }else{
			// 	localImg = config.BaseURL + imgList[b];
			// }
			let localImg;
			if(imgList[b].indexOf('http') == -1){
				if(imgList[b][0] == '/'){
					localImg = config.BaseURL + imgList[b].substr(1);
				}else{
					localImg = config.BaseURL + imgList[b];
				}
			}else{
				localImg = imgList[b];
			}

			this.imgInfo.push(localImg);
			// this.imgInfo.push(imgList[b]);
		}
		if(this.isViewer && $(".viewer-container").length >0){
			this.viewer.destroy();
		}
	}
	imgClose(modal){
		modal.hide();
	}
	bigImg:any=''// 放大的图片
	//图片放大
	bigimg(modal,row){
		// modal.show();
		this.bigImg = row
		if(this.isViewer && $(".viewer-container").length >0){
			System.import('assets/js/viewer/viewer.js').then(
				()=>{System.import('assets/js/viewer/bootstrap.bundle.min.js').then(
					()=>{System.import('assets/js/viewer/viewer.css').then(()=>{
						this.showImg();
					})
				})
			})
		}else{
			this.showImg();
		}

	}
	//显示图片组件
	viewer: any='';
	showImg(){
		var that = this;
		var imgList = $(".imagesInfo")[0];
		this.viewer = new Viewer(imgList, {
			url: 'src',
			title: function (image) {
				return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')' ; 
			},
		});
		that.isViewer = true;
	}
	CloseBigImg(modal){
		modal.hide();
	}
	// 删除登录日志
	deleteRow(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let parameters = {
				ids: row.id,
				type: "4",
			}
			this.jsonApiService.http_obj("appointmentRepairCar/batchDeleted.html", parameters).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}


	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	} 

	replyId: any='';
	replyContent :any=''; //回复内容
	appletsCode:any = ''; // 回复的不同小程序的对应的code
	//回复内容
	reply(row,modal){
		this.replyId = row.id;
		this.replyContent = '';
		this.appletsCode = row.appletsCode?row.appletsCode:""
		modal.show();
	}

	itemModalClose(modal){
		modal.hide();
	}

	itemModalSubmit(modal){
		let parameters = {
			id: this.replyId,
			replyContent: this.replyContent,
			appletsCode:this.appletsCode
		}
		this.jsonApiService.http_obj("appointmentRepairCar/addReply.html", parameters).subscribe(data => {
			this.jsonApiService.message(data.message);
			modal.hide()
			this.load();
		})
	}
	showMessage(content,title,modal){
		modal.show();
		this.title = title
		this.content = content
	}
}